package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.entity.XlsBanksEntity;
import io.renren.common.entity.XlsRealnameEntity;
import io.renren.common.entity.XlsUsersBankcardsEntity;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.XlsBanksDao;
import io.renren.dao.XlsRealnameDao;
import io.renren.dao.XlsUsersBankcardsDao;
import io.renren.dto.XlsUsersBankcardsDTO;
import io.renren.service.XlsUsersBankcardsService;
import io.renren.utils.AppResult;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户的银行卡表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-12
 */
@Service
public class XlsUsersBankcardsServiceImpl extends CrudServiceImpl<XlsUsersBankcardsDao, XlsUsersBankcardsEntity, XlsUsersBankcardsDTO> implements XlsUsersBankcardsService {

    @Autowired
    private XlsUsersBankcardsDao xlsUsersBankcardsDao;
    @Autowired
    private XlsRealnameDao xlsRealnameDao;
    @Autowired
    private XlsBanksDao xlsBanksDao;

    @Override
    public QueryWrapper<XlsUsersBankcardsEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<XlsUsersBankcardsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public AppResult toAddBankcards(Long userId) {
        Map<String, Object> map = new HashMap<>();
        if (userId.equals("") || userId == null) {
            return AppResult.ERROR("尚未登录请重新登录");
        }
        Map<String, Object> where = new HashMap<>();
        where.put("userId", userId);
        Map<String, Object> realname = xlsRealnameDao.selectRealNameByMap(where);
        if (StringUtils.isNotNull(realname)) {
            String realName = realname.get("realName") + "";
            String cardId = realname.get("cardId") + "";
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("realName", realName);
            dataMap.put("cardId", cardId);
            return AppResult.OK("1",dataMap);
        }else {
            return AppResult.OK("0",map);
        }

    }

    @Override
    public AppResult agreeconfirm(XlsUsersBankcardsEntity usersBankcards, String realName, String cardId) {
        Map<String, Object> map = new HashMap<>();
        try {

            Long userId = usersBankcards.getUserId();
            if (userId.equals("") || userId == null) {
                return AppResult.ERROR("尚未登录请重新登录");
            }
            map.put("userId",usersBankcards.getUserId());
            Map<String, Object> stringObjectMap = xlsRealnameDao.selectRealNameByMap(map);
            if (StringUtils.isNull(stringObjectMap)){
                XlsRealnameEntity newRealname = new XlsRealnameEntity();
                newRealname.setRealName(realName);
                newRealname.setCardId(cardId);
                newRealname.setUserId(usersBankcards.getUserId());
                int i = xlsRealnameDao.insert(newRealname);
                if (i < 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AppResult.ERROR("添加实名认证失败");
                }
            }
            String accNo = usersBankcards.getAccNo();
            String phone = usersBankcards.getPhone();
            String accArea = usersBankcards.getAccArea();
            Integer provinceId = usersBankcards.getProvinceId();
            Integer bankId = usersBankcards.getBankId();
            Integer areaId = usersBankcards.getAreaId();
            Integer cityId = usersBankcards.getCityId();
            XlsUsersBankcardsEntity newUsersBankcards = new XlsUsersBankcardsEntity();//创建对象用于新增或者修改
            newUsersBankcards.setAccNo(accNo);
            newUsersBankcards.setPhone(phone);
            newUsersBankcards.setProvinceId(provinceId);
            newUsersBankcards.setBankId(bankId);
            newUsersBankcards.setAreaId(areaId);
            newUsersBankcards.setCityId(cityId);

            newUsersBankcards.setAccArea(accArea);
            newUsersBankcards.setCreateTime(new Date());
            newUsersBankcards.setDataFlag(1);
            newUsersBankcards.setUserId(userId);
            newUsersBankcards.setType(1);
            int result = xlsUsersBankcardsDao.insert(newUsersBankcards);
            if (result < 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AppResult.ERROR("添加银行卡失败");
            }
//			}
            return AppResult.OK("添加银行卡成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if (e.getMessage() != null) {
                return AppResult.ERROR("运行异常");
            }
            return AppResult.ERROR("运行异常");
        }
    }

    @Override
    public AppResult selectBankCartsList(XlsUsersBankcardsEntity usersBankcards) {
        usersBankcards.setDataFlag(1);
        List<Map<String,Object>> list= xlsUsersBankcardsDao.selectUsersBankcardsList(usersBankcards);
        return AppResult.OK(list);
    }

    @Override
    @Transactional
    public AppResult unbind(Long bankCardId) {
        Map<String, Object> map=new HashMap<>();
        XlsUsersBankcardsEntity usersBankcards = xlsUsersBankcardsDao.selectById(bankCardId);
        if (StringUtils.isNull(usersBankcards)){
            return AppResult.ERROR("请选择银行卡");
        }
        try {
            XlsUsersBankcardsEntity paramUsersBankcards=new XlsUsersBankcardsEntity();
            paramUsersBankcards.setId(usersBankcards.getId());
            paramUsersBankcards.setDataFlag(-1);
            int i = xlsUsersBankcardsDao.updateById(paramUsersBankcards);
            if (i>0){
                return AppResult.OK("解除绑定成功");
            }else {
                return AppResult.ERROR("解除绑定失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage()!=null){
                map.put("msg",e.getMessage());
            }else {
                map.put("msg","运行异常");
            }
            return AppResult.ERROR(map.get("msg")+"");
        }
    }

    @Override
    public AppResult getBankById(Long bankCardId) {
        Map<String,Object> map=new HashMap<>();
        XlsUsersBankcardsEntity usersBankcards = xlsUsersBankcardsDao.selectById(bankCardId);
        if (StringUtils.isNotNull(usersBankcards)){
            XlsBanksEntity banks = xlsBanksDao.selectById(usersBankcards.getBankId());
            Map<String,Object> bank=new HashMap<>();
            bank.put("bankCardId",usersBankcards.getId());
            bank.put("bankName",banks.getBankName());
            bank.put("accNo",usersBankcards.getAccNo());
            map.put("usersBankcard",bank);
        }else {
            map.put("usersBankcard",null);
        }
        return AppResult.OK(map);
    }
}